package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;

public class consumer {
    public static void main(String[] args) throws Exception {
        //连接工厂
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.126.129");
        f.setUsername("admin");
        f.setPassword("admin");
        //建立连接
        Connection connection = f.newConnection();
        //建立通道
        Channel channel = connection.createChannel();
        //声明队列
        channel.queueDeclare("helloworld",false,false,false,null);

        DeliverCallback deliverCallback = new DeliverCallback() {

            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String msg = new String(body);
                System.out.println("收到：" + msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(java.lang.String consumerTag) throws IOException {

            }
        };
        //从helloworld队列接受消息
        channel.basicConsume("helloworld",true,deliverCallback,cancelCallback);

        System.out.println("等待接受数据");

        //收到消息后用来处理消息的回调对象
        //消费者取消时的回调对象
    }
}
